<?php

namespace app\api\controller;

use app\api\model\Send;
use think\facade\Db;
use think\Request;
use think\Response;

class Order extends Wap
{
    /**
     * 支付预约
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function addOrder(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();
            $user = $this->checkToken();
            $order = Db::name('customer_order')->where('order_on', $post['order_on'])->find();

            if ($post['type'] == 5) {
                $pricecoupon = Db::name('mini_user_pricecoupon')->where('id', $post['coupon_id'])->find();
                if ($pricecoupon['status'] > 0) {
                    $arr['code'] = 1001;
                    $arr['message'] = '当前团购券已使用';
                    Response::create($arr, 'json')->send();
                    exit();
                }

                $order['status'] = 1;
                $order['pay_time'] = time();
                $order['pay_price'] = 0;
                $order['coupon_id'] = $post['coupon_id'];
                $order['pay_type'] = 5;
                if (Db::name('customer_order')->where('order_on', $post['order_on'])->update($order)) {
                    $status['status'] = 1;
                    if (Db::name('mini_user_pricecoupon')->where('id', $post['coupon_id'])->update($status)) {
                        $arr['code'] = 200;
                        $arr['message'] = '预约成功';
                        Response::create($arr, 'json')->send();
                        exit();
                    }
                }
            }

            if ($post['type'] == 6) {
                $pricecoupon = Db::name('mini_user_cardcoupon')->where('id', $post['coupon_id'])->find();
                if ($pricecoupon['end_time'] < time() && $pricecoupon['num'] == 0) {
                    $arr['code'] = 1001;
                    $arr['message'] = '当前团购券已使用';
                    Response::create($arr, 'json')->send();
                    exit();
                }

                $order['status'] = 1;
                $order['pay_time'] = time();
                $order['pay_price'] = 0;
                $order['coupon_id'] = $post['coupon_id'];
                $order['pay_type'] = 6;
                if (Db::name('customer_order')->where('order_on', $post['order_on'])->update($order)) {
                    $status['num'] = $pricecoupon['num'] - 1;
                    $status['add_time'] = strtotime(date('Y-m-d', time())) + 86400;
                    if (Db::name('mini_user_cardcoupon')->where('id', $post['coupon_id'])->update($status)) {
                        $arr['code'] = 200;
                        $arr['message'] = '预约成功';
                        Response::create($arr, 'json')->send();
                        exit();
                    }
                }
            }

            $order['coupon_id'] = $post['coupon_id'];
            if ($post['coupon_id'] > 0) {
                // 查询优惠券
                $userCoupon = Db::name('mini_user_coupon')->where('id', $post['coupon_id'])->find();
                if ($userCoupon['status'] > 0) {
                    $arr['code'] = 1001;
                    $arr['message'] = '当前优惠券已使用';
                    Response::create($arr, 'json')->send();
                    exit();
                }
                $coupon = Db::name('coupon')->where('id', $userCoupon['coupon_id'])->find();

                $order['pay_price'] = ($order['price'] - $userCoupon['price']) > 0 ? $order['price'] - $userCoupon['price'] : 0;
                $order['update_time'] = time();

            } else {
                $order['pay_price'] = $order['price'];
            }


            if ($order['pay_price'] < 0.01) {

                $order['status'] = 1;
                $order['pay_time'] = time();

                $order['pay_type'] = $userCoupon['type'] + 1;
                if (Db::name('customer_order')->where('order_on', $post['order_on'])->update($order)) {
                    if ($post['coupon_id'] > 0) {
                        $status['status'] = 1;
                        if (Db::name('mini_user_coupon')->where('id', $post['coupon_id'])->update($status)) {
                            $arr['code'] = 200;
                            $arr['message'] = '预约成功';
                            Response::create($arr, 'json')->send();
                            exit();
                        }


                    } else {
                        $arr['code'] = 200;
                        $arr['message'] = '预约成功';
                        Response::create($arr, 'json')->send();
                        exit();
                    }

                }
            }

            switch ($post['type']) {
                case 1:
                    if (Db::name('customer_order')->where('order_on', $post['order_on'])->update($order)) {

                        $da['order_on'] = trim($post['order_on']);
                        $arr['code'] = 200;
                        $arr['data'] = $da;
                        $arr['message'] = '获取成功';
                        Response::create($arr, 'json')->send();
                        exit();
                    }

                    break;
                case 2:

                    //充值记录
                    $recharge_order = Db::name('customer_recharge_order')->where('user_id', $user['id'])->where('store_id', $order['store_id'])->where('status', 1)->find();
                    if (!$recharge_order) {
                        $user['balance'] = 0;
                    }
                    if ($user['balance'] < $order['pay_price']) {
                        $arr['code'] = 1001;
                        $arr['message'] = '余额不足';
                        Response::create($arr, 'json')->send();
                        exit();
                    }
                    $order['pay_type'] = 2;
                    $order['status'] = 1;
                    $order['balance'] = $order['pay_price'];
                    $order['pay_time'] = time();
                    $order['pay_price'] = 0;
                    $orderyz = Db::name('customer_order')->where('order_on', $post['order_on'])->find();
                    if ($orderyz['status'] > 0) {
                        $arr['code'] = 1001;
                        $arr['message'] = '订单已支付';
                        Response::create($arr, 'json')->send();
                        exit();
                    }
                    if (Db::name('customer_order')->where('order_on', $post['order_on'])->update($order)) {
                        $users['balance'] = $user['balance'] - $order['balance'];
                        Db::name('member')->where('id', $user['id'])->update($users);
                        if ($post['coupon_id'] > 0) {
                            $status['status'] = 1;
                            Db::name('mini_user_coupon')->where('id', $post['coupon_id'])->update($status);
                        }
                        Send::send_order($post['order_on']);
                        $userbalance = array(
                            'store_id' => $order['store_id'],
                            'price' => $order['balance'],
                            'type' => 2,
                            'status' => $order['order_on'],
                            'before_price' => $user['balance'],
                            'aater_price' => $users['balance'],
                            'create_time' => time(),
                            'user_id' => $user['id'],
                            'content' => "预约包厢",
                        );
                        Db::name('customer_user_balance')->insert($userbalance);
                        $arr['code'] = 200;
                        $arr['message'] = '预约成功';
                        Response::create($arr, 'json')->send();
                        exit();

                    }
                    break;
            }
        }
    }


    /**
     * 刪除訂單
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function delete(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();

            $mark['mark'] = 0;
            $mark['update_time'] = time();
            if (Db::name('customer_order')->where('order_on', $post['order_on'])->where('status', 0)->update($mark)) {
                Db::name('customer_order_cleaning')->where('order_on', $post['order_on'])->where('status', 0)->delete();
                $arr['code'] = 200;
                $arr['message'] = '刪除成功';
                Response::create($arr, 'json')->send();
                exit();
            } else {
                $arr['code'] = 101;
                $arr['message'] = '订单不能删除';
                Response::create($arr, 'json')->send();
                exit();
            }

        }
    }


    /**
     * 用户订单列表
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function UserOrder(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();
            $user = $this->checkToken();

            switch ($post['status']) {
                case 0:
                    $list = Db::name('customer_order')->where('order_type', 1)->where('user_id', $user['id'])->order('create_time desc')->where('mark', 1)->limit(0, 10)->select();
                    break;
                case 1:
                    $list = Db::name('customer_order')->where('status', 1)->where('order_type', 1)->order('create_time desc')->where('user_id', $user['id'])->where('mark', 1)->select();
                    break;
                case 2:
                    $list = Db::name('customer_order')->where('status', 2)->where('order_type', 1)->order('create_time desc')->where('user_id', $user['id'])->where('mark', 1)->select();
                    break;
                case 4:
                    $list = Db::name('customer_order')->where('status', 4)->where('order_type', 1)->order('create_time desc')->where('user_id', $user['id'])->limit(0, 10)->where('mark', 1)->select();
                    break;
                case 3:
                    $list = Db::name('customer_order')->where('status', 3)->where('order_type', 1)->order('create_time desc')->where('user_id', $user['id'])->limit(0, 5)->where('mark', 1)->select();
                    break;

            }

            $data = array();
            foreach ($list as $i => $v) {
                $store = Db::name('mini_room_list')->where('id', $v['store_id'])->find();

                $course = Db::name('customer_course')->where('id', $v['seat_id'])->find();
                $package = Db::name('customer_package')->where('id', $v['package_id'])->find();
                $door_list = array();
                if ($course['door_id'] > 0) {
                    $door = Db::name('customer_door_list')->where('id', $course['door_id'])->find();

                    if ($door['pid'] > 0) {
                        $doord = Db::name('customer_door_list')->where('id', $door['pid'])->find();
                        $door_list[] = array(
                            'door_id' => $doord['door_id'],
                            'door_name' => $doord['name'],
                        );
                    }
                    $door_list[] = array(
                        'door_id' => $door['door_id'],
                        'door_name' => $door['name'],
                    );
                }
                $v['address_lan'] = '';
                if (isset($store['province_code'])) {
                    if ($store['province_code']) {
                        if ($store['province_code']) {
                            $store['province_name'] = Db::name('city')->where('citycode', $store['province_code'])->find();
                        }
                        if ($store['city_code']) {
                            $store['city_name'] = Db::name('city')->where('citycode', $store['city_code'])->find();
                        }
                        if ($store['district_code'] > 0) {
                            $store['district_name'] = Db::name('city')->where('citycode', $store['district_code'])->find();
                            if ($store['district_name']) {
                                if ($store['district_name'] == "") {
                                    $store['district_name']['name'] = ' ';
                                }
                            }
                        } else {
                            $store['district_name']['name'] = ' ';
                        }

                        $v['address_lan'] = $store['province_name']['name'];
                        if (isset($store['province_name']['name'])) {
                            if (isset($store['city_name']['name'])) {
                                $v['address_lan'] = $store['province_name']['name'] . $store['city_name']['name'];
                            }
                            if (isset($store['district_name']['name'])) {
                                $v['address_lan'] = $store['province_name']['name'] . $store['city_name']['name'] . $store['district_name']['name'] . $store['address'];

                            }
                        }


                    }
                }

                if ($store['head_url']) {
                    $store['head_url'] = get_image_url($store['head_url']);
                }
                $data[] = array(
                    'order_on' => $v['order_on'],
                    'store_name' => $store['room_name'],
                    'name' => $course['name'],
                    'mobile' => $store['mobile'],
                    'create_time' => date('Y.m.d H:i:s', $v['create_time']),
                    'add_time' => date('Y.m.d H:i:s', $v['add_time']) . "~" . date('H:i', $v['end_time']),
                    'order_type' => 1,
                    'package_name' => $package['name'],
                    'price' => $v['pay_price'],
                    'status' => $v['status'],
                    'door_list' => $door_list,
                    'lat' => $store['latitude'],
                    'lng' => $store['longitude'],
                    'address' => $v['address_lan'],
                    'head_url' => $store['head_url'],
                    'fz' => $store['sub_mch_id'] ? 1 : 0,
                );
            }
            $arr['code'] = 200;
            $arr['data'] = $data;
            $arr['message'] = '获取成功';
            Response::create($arr, 'json')->send();
            exit();

        }
    }


    /**
     * 首页开门开灯
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function userOrderInfo(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();
            $user = $this->checkToken();
            $order = Db::name('customer_seat_order')->where('user_id', $user['id'])->where('status', 'in', [1, 2])->find();
            $data = array();
            if ($order) {
                $house_info = Db::name('customer_house')->where('id', $order['house_id'])->find();
                if ($house_info['door_id']) {
                    $door = Db::name('customer_door_list')->where('id', $house_info['door_id'])->find();
                    if ($door['pid'] > 0) {
                        $doord = Db::name('customer_door_list')->where('id', $door['pid'])->find();
                        $door_list[] = array(
                            'door_id' => $doord['door_id'],
                            'door_name' => $doord['name'],
                        );
                    }
                    $door_list[] = array(
                        'door_id' => $door['door_id'],
                        'door_name' => $door['name'],
                    );
                }
                $data[] = array(
                    'door_list' => $door_list,
                    'order_type' => 1,
                    'order' => 1,
                    'order_on' => $order['order_on'],
                );
            } else {
                $order = Db::name('customer_order')->where('user_id', $user['id'])->where('status', 'in', [1, 2])->select();


                foreach ($order as $i => $v) {
                    $course = Db::name('customer_course')->where('id', $v['seat_id'])->find();
                    $door_list = array();
                    if ($course['door_id'] > 0) {
                        $door = Db::name('customer_door_list')->where('id', $course['door_id'])->find();
                        if ($door['pid'] > 0) {
                            $doord = Db::name('customer_door_list')->where('id', $door['pid'])->find();
                            $door_list[] = array(
                                'door_id' => $doord['door_id'],
                                'door_name' => $doord['name'],
                            );
                        }
                        $door_list[] = array(
                            'door_id' => $door['door_id'],
                            'door_name' => $door['name'],
                        );

                    }

                    $data[] = array(
                        'door_list' => $door_list,
                        'order_type' => 2,
                        'order' => count($order),
                        'order_on' => $v['order_on'],
                    );
                }

            }
            $arr['code'] = 200;
            $arr['data'] = $data;
            $arr['message'] = '获取成功';
            Response::create($arr, 'json')->send();
            exit();
        }
    }


    /**
     * 续单套餐
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function renewal(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();
            $user = $this->checkToken();
            $order = Db::name('customer_order')->where('order_on', $post['order_on'])->find();

            $course = Db::name('customer_course')->where('id', $order['seat_id'])->find();
            $package = $this->getpackage($course['id']);
            $data = array();
            $data['order'] = array(
                'end_time' => $order['end_time'],
                'name' => $course['name'],
            );
            foreach ($package as $i => $v) {
                $data['renewal'][] = array(
                    'name' => $v['name'],
                    'happy_id' => $v['id'],
                    'price' => $v['price'],
                    'time' => $v['time'],
                );
            }
            $arr['code'] = 200;
            $arr['data'] = $data;
            $arr['message'] = '获取成功';
            Response::create($arr, 'json')->send();
            exit();

        }
    }

    /**
     * @param $userId
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getpackage($userId)
    {
        $roleList = Db::name('customer_course_package_id')->alias("ur")
            ->field('r.name,r.price,r.time,r.id')
            ->join('customer_package r', 'ur.package_id=r.id')
            ->distinct(true)
            ->where('ur.course_id', '=', $userId)
            ->where('r.type', '=', 2)
            ->select()->toArray();
        return $roleList;
    }


    /**
     * 查看是否预约
     * @param $store_id
     * @param $add_time
     * @param $end_time
     * @return int
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function repeat($store_id, $add_time, $end_time)
    {
        $order = Db::name('customer_order')->where('seat_id', $store_id)->where('status', 'in', [0, 1, 2])->select();
        $status = array();
        if (count($order) > 0) {

            foreach ($order as $i => $v) {
                $add_hour = $v['add_time'];
                $end_hour = $v['end_time'];
                if ($this->is_time_cross($add_hour, $end_hour, $add_time, $end_time)) {
                    $status[] = '1';
                } else {
                    $status[] = '0';
                }
            }
        }
        if (in_array('1', $status, TRUE)) {
            $orderStatus = 1;
        } else {
            $orderStatus = 0;
        }
        return $orderStatus;
    }

    /**
     * 续单
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function renewalOrder(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();
            $user = $this->checkToken();
            if ($user['status'] == 1) {
                $arr['code'] = 1001;
                $arr['message'] = '该账号被封禁';
                Response::create($arr, 'json')->send();
                exit();
            }
            $order = Db::name('customer_order')->where('order_on', $post['order_on'])->find();
            $room = Db::name('mini_room_list')->where('id', $order['store_id'])->find();

            $package = Db::name('customer_package')->where('id', $post['happy_id'])->where('mark', 1)->find();
            // 判断是否重复预约
            $orderStatus = $this->repeat($order['seat_id'], $order['end_time'], ($order['end_time']) + $package['time'] * 3600);
            if ($orderStatus > 0) {
                $arr['code'] = 1001;
                $arr['message'] = '当前已有预约，不可重复预约';
                Response::create($arr, 'json')->send();
                exit();
            }
            $data = array(
                'order_on' => $this->orderOn(),
                'store_id' => $order['store_id'],
                'seat_id' => $order['seat_id'],
                'add_time' => ($order['end_time']),
                'end_time' => ($order['end_time']) + $package['time'] * 3600,
                'price' => $package['price'],
                'shi' => $package['time'],
                'user_id' => $user['id'],
                'create_time' => time(),
                'package_id' => $post['happy_id'],
                'order_renewal_id' => $order['id'],
                'if_fz' => $room['sub_mch_id'] == null ? '0' : '1',
            );
            if (Db::name('customer_renewal_order')->insert($data)) {

                $da['order_on'] = $data['order_on'];
                $da['store_id'] = $data['store_id'];
                $arr['code'] = 200;
                $arr['data'] = $da;
                $arr['message'] = '获取成功';
                Response::create($arr, 'json')->send();
                exit();
            }
        }
    }


    /**
     * 续单详情
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function renewalOrderInfo(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();
            $user = $this->checkToken();
            $order = Db::name('customer_renewal_order')->where('order_on', $post['order_on'])->find();
            $info = Db::name('customer_course')->where('id', $order['seat_id'])->find();
            $package = Db::name('customer_package')->where('mark', 1)->where('id', $order['package_id'])->find();

            $store = Db::name('mini_room_list')->where('id', $info['store_id'])->find();
            $data['order'] = array(
                'order_on' => $order['order_on'],
                'price' => $order['price'],
                'name' => $info['name'],
                'store_name' => $store['room_name'],
                'add_time' => date('Y.m.d H:i:s', $order['add_time']) . '~~' . date('Y.m.d H:i:s', $order['end_time']),
                'package_name' => $package['name'],
                'head_url' => get_image_url($store['head_url']),
                'store_id' => $info['store_id']
            );
            $data['user'] = array(
                'balance' => $user['balance']
            );
            $data['fz'] = $store['sub_mch_id'] ? 1 : 0;
            $arr['code'] = 200;
            $arr['data'] = $data;
            $arr['message'] = '获取成功';
            Response::create($arr, 'json')->send();
            exit();
        }
    }


    /**
     * 续单支付
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function renewalAddorder(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();
            $user = $this->checkToken();
            $order = Db::name('customer_renewal_order')->where('order_on', $post['order_on'])->find();

            $order['pay_price'] = $order['price'];
            if ($post['coupon_id'] > 0) {
                // 查询优惠券
                $userCoupon = Db::name('mini_user_coupon')->where('id', $post['coupon_id'])->find();
                if ($userCoupon['status'] > 0) {
                    $arr['code'] = 1001;
                    $arr['message'] = '当前优惠券已使用';
                    Response::create($arr, 'json')->send();
                    exit();
                }
                //    $order['pay_price'] = $order['price'] - $userCoupon['price'];

                $order['pay_price'] = ($order['price'] - $userCoupon['price']) > 0 ? $order['price'] - $userCoupon['price'] : 0;

            }
            switch ($post['type']) {
                case 1:
                    $order['coupon_id'] = $post['coupon_id'];
                    $order['update_time'] = time();
                    if (Db::name('customer_renewal_order')->where('order_on', $post['order_on'])->update($order)) {
                        $da['order_on'] = trim($post['order_on']);
                        $arr['code'] = 200;
                        $arr['data'] = $da;
                        $arr['message'] = '获取成功';
                        Response::create($arr, 'json')->send();
                        exit();
                    }
                case 2:
                    if ($user['balance'] < $order['pay_price']) {
                        $arr['code'] = 1001;
                        $arr['message'] = '余额不足';
                        Response::create($arr, 'json')->send();
                        exit();
                    }
                    $order['pay_type'] = 2;
                    $order['status'] = 1;
                    $order['coupon_id'] = $post['coupon_id'];
                    //  $order['balance']=$order['pay_price'];

                    if (Db::name('customer_renewal_order')->where('order_on', $post['order_on'])->update($order)) {



                        $orderInfo = Db::name('customer_order')->where('id', $order['order_renewal_id'])->find();

                        $orderInfo['end_time'] = $order['end_time'];
                        $orderInfo['balance'] = $orderInfo['balance'] + $order['balance'];
                        $orderInfo['shi'] = $orderInfo['shi'] + $order['shi'];
                        $orderInfo['is_voice_end'] = 0;
                        $orderInfo['is_voice'] = 0;

                        Db::name('customer_order')->where('id', $orderInfo['id'])->update($orderInfo);

                        $course = Db::name('customer_course')->where('id', $orderInfo['seat_id'])->find();
                        $door = Db::name('customer_door_list')->where('id', $course['door_id'])->find();

                        if ($door['door_type'] == 2) {
                            $res = self::mqttopenone($door['door_id'], $order['end_time'] - time());
                        }

                        $users['balance'] = $user['balance'] - $order['pay_price'];
                        Db::name('member')->where('id', $user['id'])->update($users);
                        if ($post['coupon_id'] > 0) {
                            $status['status'] = 1;
                            Db::name('mini_user_coupon')->where('id', $post['coupon_id'])->update($status);
                        }

                        // 更新保洁
                        $cleaning = Db::name('customer_order_cleaning')->where('order_on', $orderInfo['order_on'])->find();
                        if ($cleaning) {
                            $room = Db::name('mini_room_list')->where('id', $orderInfo['store_id'])->find();
                            $cleaning['add_time'] = $orderInfo['end_time'];
                            $cleaning['end_time'] = $orderInfo['end_time'] + $room['time'] * 60;
                            Db::name('customer_order_cleaning')->where('order_on', $orderInfo['order_on'])->update($cleaning);
                        }
                        $userbalance = array(
                            'store_id' => $order['store_id'],
                            'price' => $order['pay_price'],
                            'type' => 2,
                            'status' => $order['order_on'],
                            'before_price' => $user['balance'],
                            'aater_price' => $users['balance'],
                            'create_time' => time(),
                            'user_id' => $user['id'],
                            'content' => "预约续费",

                        );
                        Db::name('customer_user_balance')->insert($userbalance);
                        $arr['code'] = 200;
                        $arr['message'] = '预约成功';
                        Response::create($arr, 'json')->send();
                        exit();

                    }
                    break;
            }
        }
    }


    /**
     * 分享
     * @param Request $request
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function ShareOrder(Request $request)
    {
        if (request()->isPost()) {
            $post = $request->param();

            $order = Db::name('customer_order')->where('order_on', $post['order_on'])->where('status', 'in', [1, 2])->find();

            if (!$order) {
                $arr['code'] = 101;

                $arr['message'] = '订单已完成';
                Response::create($arr, 'json')->send();
                exit();
            }
            $user = Db::name('member')->where('id', $order['user_id'])->find();
            $store = Db::name('mini_room_list')->where('id', $order['store_id'])->find();
            $course = Db::name('customer_course')->where('id', $order['seat_id'])->find();

            $package = Db::name('customer_package')->where('id', $order['package_id'])->find();
            $door_list = array();
            if ($course['door_id'] > 0) {
                $door = Db::name('customer_door_list')->where('id', $course['door_id'])->find();
                if ($door['pid'] > 0) {
                    $doord = Db::name('customer_door_list')->where('id', $door['pid'])->find();
                    $door_list[] = array(
                        'door_id' => $doord['door_id'],
                        'door_name' => $doord['name'],
                    );
                }
                $door_list[] = array(
                    'door_id' => $door['door_id'],
                    'door_name' => $door['name'],
                );
            }
            if ($store['province_code']) {


                if ($store['province_code']) {
                    $store['province_name'] = Db::name('city')->where('citycode', $store['province_code'])->find();
                }
                if ($store['city_code']) {
                    $store['city_name'] = Db::name('city')->where('citycode', $store['city_code'])->find();
                }
                if ($store['district_code'] > 0) {
                    $store['district_name'] = Db::name('city')->where('citycode', $store['district_code'])->find();
                    if ($store['district_name']) {
                        if ($store['district_name'] == "") {
                            $store['district_name']['name'] = ' ';
                        }

                    }
                } else {
                    $store['district_name']['name'] = ' ';
                }


                $store['address_lan'] = $store['province_name']['name'] . $store['city_name']['name'] . $store['district_name']['name'] . $store['address'];


            }
            $data = array(
                'order_on' => $order['order_on'],
                'store_name' => $store['room_name'],
                'name' => $course['name'],
                'create_time' => date('Y.m.d H:i:s', $order['create_time']),
                'add_time' => date('Y.m.d H:i:s', $order['add_time']) . "~" . date('H:i', $order['end_time']),
                'order_type' => 1,
                'package_name' => $package['name'],
                'price' => $order['pay_price'],
                'status' => $order['status'],
                'door_list' => $door_list,
                'lat' => $store['latitude'],
                'lng' => $store['longitude'],
                'address' => $store['address_lan'],
                'head_url' => $store['head_url'],
                'title' => '您的好友' . $user['nickname'] . '邀请您在' . $store['room_name'] . '-' . $course['name'] . '相聚，请您准时参加',
            );

            $arr['code'] = 200;
            $arr['data'] = $data;
            $arr['message'] = '获取成功';
            Response::create($arr, 'json')->send();
            exit();
        }
    }
}